<template>
  <div class="edit-adress">
    <m-header modile-title="地址选择" go-back="true"></m-header>
    <div class="page-content">
      <mt-cell title="省市区:" :value="areaString" is-link @click.native="handlerArea"></mt-cell>
      <mt-popup v-model="areaVisible" class="area-class" position="bottom">
        <div class="btn_list">
          <button class="btn_one" @click="cancelBtn()">取消</button>
          <button class="btn_two" @click="sureBtn()">确定</button>
        </div>
        <mt-picker :slots="slots" @change="onValuesChange"></mt-picker>
      </mt-popup>
    </div>
    <div class="btn">
      <div class="button" @click="OkBtn()">确认</div>
    </div>
  </div>
</template>

<script>
  import mHeader from '../../../components/header'
  import {Toast} from 'mint-ui'
  // import data from '../../../static/data.json'
  import {getdata} from "../../../api/data"
  import qs from 'qs'

  let index = 0 // 默认设置二级的index
  let index2 = 0 // 默认设置三级的index
  let p = []
  let c = []
  let a = []
  export default {
    name: 'EditAdress',
    data() {
      return {
        lists: [],
        areaVisible: false,
        areaString: '请选择',
        array: [],
        code: '',
        keepArea: '',
        slots: [{
          flex: 1,
          values: p,
          className: 'slot1',
          textAlign: 'center'
        }, {
          divider: true,
          content: '-',
          className: 'slot2'
        }, {
          flex: 1,
          values: c,
          className: 'slot3',
          textAlign: 'center'
        }, {
          divider: true,
          content: '-',
          className: 'slot4'
        }, {
          flex: 1,
          values: a,
          className: 'slot5',
          textAlign: 'center'
        }]
      }
    },
    created() {
      this._getdata()
    },
    mounted() {
      setTimeout(() => {
        // console.log(this.lists)
        this.lists.map(res => {
          p.push(res.name)
          // console.log(p)
        })
        this.lists.map(res => {
          res.children.map(res => {
            c.push(res.name)
            // console.log(res.name)
          })
        })
        this.lists.map(res => {
          res.children.map(res => {
            a.push(res.name)
            // console.log(res.children)
          })
        })
      }, 500)
    },
    methods: {
      _getdata() {
        getdata().then(res => {
          // console.log(res)
          if (res.code === 200) {
            this.lists = res.data
            // this.lists.push(res.data)
            // console.log(this.lists)
          }
        })
      },
      cancelBtn() {
        this.areaVisible = false
      },
      sureBtn() {
        this.areaString = this.keepArea
        this.areaVisible = false
        console.log(this.code)
      },
      handlerArea() {
        this.areaVisible = true
      },
      onValuesChange(picker, values) {
        let one = values[0]
        let two = values[1]
        let three = values[2]
        index = p.indexOf(one) // 省
        if (index >= 0 && p.length > 0) {
          c = this.lists[index].children.map(res => { //  市
            return res.name
          })
          picker.setSlotValues(1, c)
          two = values[1]
        }

        index2 = c.indexOf(two) // 区
        if (index2 >= 0 && c.length > 0) {
          a = this.lists[index].children[index2].children.map(res => {
            return [res.name, res.id];
          })
          let newarea = [];
          for (var i = 0; i < Object.values(a).length; i++) {
            newarea.push(Object.values(a)[i][0])
            if (values[2] === Object.values(a)[i][0]) {
              this.code = Object.values(a)[i][1]
            }
          }
          picker.setSlotValues(2, newarea)
          three = values[2]
        }
        this.keepArea = values.join('-') // 拼接字符串（选中好的值以-连接）
      },
      OkBtn() {
        console.log(this.keepArea, this.code)
      }
    },
    components: {
      mHeader
    }
  }
</script>

<style lang='scss'>
  .edit-adress {
    background-color: #fff;
    margin-top: 44px;
    .content {
      padding-top: 40px;
      margin: 0 10px;
      .same,
      .address {
        height: 44px;
        line-height: 44px;
        font-size: 15px;
        border-bottom: 1px solid #f1f1f1;
        input {
          outline: none;
          border: none;
        }
      }
      .same:last-child {
        border-bottom: 1px solid transparent;
      }
      .address {
        .city {
          font-size: 12px;
        }
        input:disabled {
          background-color: #fff;
        }
        input::-webkit-input-placeholder {
          color: #0078de;
        }
        input:-moz-placeholder {
          color: #0078de;
        }
        input::-moz-placeholder {
          color: #0078de;
        }
        input:-ms-input-placeholder {
          color: #0078de;
        }
        .icon {
          width: 44px;
          height: 44px;
          float: right;
          text-align: right;
          .go-icon {
            display: inline-block;
            width: 8px;
            height: 14px;
            background-size: 8px 14px;
            background-repeat: no-repeat;
            vertical-align: middle;
          }
        }
      }
    }
    .btn {
      width: 100%;
      margin-top: 50px;
      .button {
        width: 90%;
        height: 40px;
        background: #007aff;
        outline: none;
        box-shadow: none;
        font-size: 15px;
        color: #fff;
        text-align: center;
        line-height: 40px;
        margin-left: 5%;
      }
    }
  }

  .sure-cancle {
    border-bottom: 1px solid #f1f1f1;
    padding: 15px 10px;
    span {
      font-size: 15px;
      &:nth-of-type(2) {
        color: #0078de;
        float: right;
      }
    }
  }

  .mint-popup {
    width: 100%;
  }

  .picker-item {
    font-size: 15px;
  }

  .picker-item.picker-selected {
    color: #0078de;
  }

  .btn_list {
    width: 100%;
    height: 40px;
    border-bottom: 1px solid #f2f2f2;
    .btn_one, .btn_two {
      width: 120px;
      height: 39px;
      background: #fff;
      outline: none;
      border: none;
      font-size: 15px;
    }
    .btn_one {
      float: left;
    }
    .btn_two {
      float: right;
      color: #007aff;
    }
  }
</style>
